package com.msmall.base;

import java.util.Collections;
import java.util.List;

@SuppressWarnings("rawtypes")
public class PaginationSupport implements java.io.Serializable {

	private static final long serialVersionUID = 7030048407899258634L;

	public final static int PAGESIZE = 10;

	protected int pageSize = PAGESIZE; //每页显示记录数

	protected List items = Collections.emptyList(); // 记录列表
	
	protected int totalCount = 0; // 共多少条记录
	
	protected int startIndex = 0; // 当前页的开始记录号
	
	protected int pageIndex = 1; // 当前第几页；例如pageIndex=1;pageSize=5;startIndex=5;indexes[0]=0,indexes[1]=5
	
	protected int totalPage = 0; // 共多少页

	
	
	public PaginationSupport() {
	}
	
	public PaginationSupport(int pageIndex, int pageSize) {
		this.setPageIndex(pageIndex);
		this.setPageSize(pageSize);
	}
	
	public List getItems() {
		return items;
	}

	public void setItems(List items) {
		this.items = items;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		if(pageSize <= 0)
			pageSize = PAGESIZE;
		else
			this.pageSize = pageSize;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		int totalPage = 0;
		if (totalCount > 0) {
			this.totalCount = totalCount;
			totalPage = totalCount / pageSize;
			if (totalCount % pageSize > 0)
				totalPage++;
		} else {
			this.totalCount = 0;
		}
		this.setTotalPage(totalPage);
	}
	
	
	public int getStartIndex() {
		/*
		if(pageIndex < 0){
			return 0;
		}else{
			return pageIndex*pageSize;
		}
		*/
		if(pageIndex < 1){
			return 0;
		}else{
			return (pageIndex - 1)*pageSize;
		}
	}

	public void setStartIndex(int startIndex) {
		if (startIndex < 0)
			this.startIndex = 0;
		else
			this.startIndex = startIndex;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		if(pageIndex < 1){
			pageIndex = 1;
		}else{
			this.pageIndex = pageIndex;
		}
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

}
